
/******************************************************/
/*     misc_funcs.cpp     Miscellaneous functions     */
/*                        By: Josh Fiorio             */
/******************************************************/

#include "misc_funcs.h"

extern FILE *ROM;           /* TODO: make lof function get passed a file argument */
extern int rom_pos;         /* TODO: somehow get passed this to the func. as well */

long jntohl(long netlong)
{
	int BYTE1, BYTE2, BYTE3, BYTE4;

	BYTE1 = (netlong & 0x000000FF);
	BYTE2 = (netlong & 0x0000FF00) >> 8;
	BYTE3 = (netlong & 0x00FF0000) >> 16;
	BYTE4 = (netlong & 0xFF000000) >> 24;

	long jret;
	jret = (BYTE1 << 24) + (BYTE2 << 16) + (BYTE3 << 8) + BYTE4;

	return jret;
}	//end of func

int jntohs(int netshort)
{
	int BYTE1, BYTE2;

	BYTE1 = (netshort & 0x00FF) << 8;
	BYTE2 = (netshort & 0xFF00) >> 8;

	int jret;
	jret = BYTE1+BYTE2;

	return jret;
}	//end of func

short jhtons(short netshort)
{
	short BYTE1, BYTE2;

	BYTE1 = (netshort & 0x00FF) << 8;
	BYTE2 = (netshort & 0xFF00) >> 8;

	short jret;
	jret = BYTE1+BYTE2;

	return jret;
}	//end of func

long jhtonl(long netlong)
{
	long BYTE1, BYTE2, BYTE3, BYTE4;

	BYTE1 = (netlong & 0x000000FF);
	BYTE2 = (netlong & 0x0000FF00) >> 8;
	BYTE3 = (netlong & 0x00FF0000) >> 16;
	BYTE4 = (netlong & 0xFF000000) >> 24;

	long jret;
	jret = (BYTE1 << 24) + (BYTE2 << 16) + (BYTE3 << 8) + BYTE4;

	return jret;
}	//end of func


char *jsbstr(char *theStr, int theSta, int theEnd)
{	
	char cr;
	char *newStr=(char *)malloc(100);
	for (int lp=0; lp < theEnd; lp++)
	{
		cr=*(theStr+theSta+lp);
		*(newStr+lp)=cr;
	}	//end for

	*(newStr+theEnd)='\0';
	return newStr;
}	//end of func.

long lof()
{
      fseek( ROM, 0L, SEEK_END ); 
      long it=ftell(ROM); 
      fseek( ROM, rom_pos, 0); 
      return it;
}     //end of func.
